package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class KeChengLessonUplController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @Autowired
    YxyLessonsSchoolCatalogRepository schoolCatalogRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/upd/lesson", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String lessonName = reqModel.lessonName;
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;

        YxyLessonsSchoolLesson lesson = lessonRepository.findById(lessonId);

        if (null != lesson) {

            // 二级栏目为空
            if (StringUtils.isBlank(catalogId2)) {
                // 获取二级栏目，修改为选择二级栏目，不可以修改
                List<YxyLessonsSchoolCatalog> catalogs = schoolCatalogRepository.findByPid(catalogId1);
                Assert.isTrue(catalogs.size() < 0, "请选择二级栏目");
            }

            lesson.setName(lessonName);
            lesson.setCatalogId1(catalogId1);
            lesson.setCatalogId2(catalogId2);
            lessonRepository.save(lesson);
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String lessonName;
        public String catalogId1;
        public String catalogId2;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "课程不能为空");
            Assert.isTrue(StringUtils.isNotBlank(lessonName), "请输入1-35个字的课程名称");
            lessonName = lessonName.trim();
            Assert.isTrue(lessonName.length() > 0, "请输入1-35个字的课程名称");
            Assert.isTrue(StringUtils.isNotBlank(catalogId1), "一级栏目不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
